﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using C1.Win.C1FlexGrid;

namespace TC4T_WinForm
{
    public partial class HelloWorldForm : TCForm
    {
        ItemDisplayName items;
        Dictionary<string, List<string>> _typeProperties = new Dictionary<string, List<string>>();
        public HelloWorldForm()
        {
            InitializeComponent();
            items = TCHelper.GetItemHyrachy("Folder");//("Item");
            _typeProperties = TCHelper.GetTypePropertyPairByTypeNames(items.BOName.ToArray());//new string[] { "My2_TestItem" });
        }

        private void HelloWorldForm_Load(object sender, EventArgs e)
        {
            // layout
            flex.Rows.Count = 1;
            flex.Cols.Count = 2;
            flex.Cols.Fixed = 0;
            flex[0, 0] = "对象";
            flex[0, 1] = "属性";
            flex.Cols[1].Format = "c";

            // styles
            CellStyle cs = flex.Styles.Normal;
            cs.Border.Direction = BorderDirEnum.Vertical;
            cs.TextAlign = TextAlignEnum.LeftCenter;
            cs.WordWrap = false;
            cs = flex.Styles.Add("Object");
            //cs.BackColor = Color.FromArgb(234, 236, 245);
            cs.Border.Direction = BorderDirEnum.Horizontal;
            cs.ForeColor = SystemColors.InfoText;
            cs = flex.Styles.Add("Property");
            //cs.BackColor = Color.FromArgb(255, 213, 141);
            //cs.Font = new Font(flex.Font, FontStyle.Bold);

            // outline tree
            flex.Tree.Column = 0;
            flex.Tree.Style = TreeStyleFlags.Simple;
            flex.Tree.LineStyle = System.Drawing.Drawing2D.DashStyle.Solid;
            flex.AllowMerging = AllowMergingEnum.Nodes;

            // other
            flex.AllowResizing = AllowResizingEnum.Columns;
            flex.SelectionMode = SelectionModeEnum.Cell;
            flex.HighLight = HighLightEnum.Always;
            flex.FocusRect = FocusRectEnum.Solid;
            flex.AllowSorting = AllowSortingEnum.None;
            //_allowDragging = true;

            // read XML document into the grid
            if (items != null)
            {
                PopulateGrid();
                UpdateIcons();
                flex.AutoSizeCols();
            }
        }

        private void PopulateGrid()
        {
            if (_typeProperties != null)
            {
                
                int row = 0;
                foreach (KeyValuePair<string, List<string>> pair in
                    _typeProperties)
                {
                    row = flex.Rows.Count;
                    flex.Rows.Add();
                    flex[row, 0] = pair.Key;
                    flex.Rows[row].IsNode = true;
                    flex.Rows[row].Node.Level = 0;
                    Node node = flex.Rows[row].Node;
                    foreach (string s in pair.Value)
                    {
                        row = flex.Rows.Count;
                        flex.Rows.Add();
                        flex[row, 0] = s;
                        flex[row, 1] = pair.Key;
                        flex.Rows[row].IsNode = true;
                        flex.Rows[row].Node.Level = 1;
                        flex.SetCellStyle(row, 1, flex.Styles["Object"]);
                    }
                    node.Collapsed = true;
                }
            }
            // skip comment nodes
            //if (node.NodeType == XmlNodeType.Comment)
            //    return;

            //// add new row for this node
            //int row = flex.Rows.Count;
            //flex.Rows.Add();
            //if (node.Name.Equals("Property"))
            //    flex[row, 0] = node.Attributes["Name"].Value;
            //else
            //    flex[row, 0] = node.Name;
            //if (node.ChildNodes.Count == 1)
            //{
            //    flex[row, 1] = node.InnerText;
            //    flex.SetCellStyle(row, 1, flex.Styles["Data"]);
            //}

            //// make new row a node
            //flex.Rows[row].IsNode = true;
            //flex.Rows[row].Node.Level = level;

            //// if this node has children, get them as well
            //if (node.ChildNodes.Count > 1)
            //{
            //    // recurse to get children
            //    foreach (XmlNode child in node.ChildNodes)
            //        GetXMLData(child, level + 1);
            //}
        }
        private void UpdateIcons()
        {
            if (flex.Rows.Count > 0)
            {
                for (int i = 1; i < flex.Rows.Count; i++)
                {
                    if (flex.Rows[i].Node.Children < 1)
                    {

                        flex.SetCellCheck(i, 0, CheckEnum.None);
                        flex.SetCellImage(i, 0, imageList1.Images[0]);

                    }
                    else
                        flex.SetCellImage(i, 0, imageList1.Images[1]);
                }
            }
        }

        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                items = TCHelper.GetItemHyrachy(textBox1.Text.Trim());//"Folder");//("Item");
                if (items != null && items.BOName != null)
                {
                    _typeProperties = TCHelper.GetTypePropertyPairByTypeNames(items.BOName.ToArray());//new string[] { "My2_TestItem" });


                    HelloWorldForm_Load(null, null);
                }
                else
                {
                    MessageBox.Show("无此Type！");
                }
            }
        }
    }
}
